package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class GetPersonalInformationController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(GetPersonalInformationController.class);
	/**
	 * 获取“我的”页面信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getMyInformation", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getMyInformation(HttpServletRequest request)
			throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse();
		
		try {
			//用户Id
			String userId = request.getParameter("userId");
			
			Map<String, Object> param = new HashMap<String, Object>();
			if(null != userId && !userId.equals("")){
				param.put("userId", userId);
				param.put("imgUrl", Config.getImageAddress());
				// 查询信息
				Map<String, Object> listData = bizService.getMyInformationById(param);
				if(null!=listData&&listData.size()>0){
					response.setData(listData);
				}else{
					response.setErrorcode(1);
					response.setMessage("无该学员信息");
				}
			}else{
				response.setErrorcode(1);
				response.setReturnMsg(1, "缺少必填字段！");
			}

		} catch (Exception e) {
			logger.info("获取个人信息："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取个人信息失败!");
		} finally {
			return response.toJSON();
		}

	}

	/**
	 * 获取个人信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getPersonalInformation", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getPersonalInformation(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		try {

			// 学员电话
			String studentTel = request.getParameter("studentTel");
			Map<String, Object> result = new HashMap<String, Object>();
			Map<String, Object> param = new HashMap<String, Object>();
			
			if(null!=studentTel&&!studentTel.equals("")){
				param.put("studentTel", studentTel);
				param.put("imgUrl", Config.getImageAddress());
				// 查询报名意向信息
				Map<String, Object> listData = bizDao.getPersonalInformationByTel(param);
				if(null!=listData&&listData.size()>0){
					// 设置数据集合
					response.setData(listData);
				}else{
					result.put("BName", "");
					result.put("FamilyAddress", "");
					result.put("IDCardNo", "");
					result.put("Mobilephone", "");
					result.put("Name", "");
					result.put("SecondName", "");
					result.put("Pic", "");
					result.put("id", 0);
					response.setData(result);
					response.setMessage("该学员无个人信息,未绑定！");
				}
				
			}else{
				response.setErrorcode(1);
				response.setReturnMsg(1, "缺少必填字段！");
			}

		} catch (Exception e) {
			logger.info("获取个人信息："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取个人信息失败!");
		} finally {
			return response.toJSON();
		}

	}

}
